home *** CD-ROM | disk | FTP | other *** search
- /*
- * DemoSignedObjectDialog.c
- * Copyright © 1992-93 Apple Computer Inc. All rights reserved.
- * This sub-class to CDialog demonstrates how the Digital
- * Signature Manager may be used to sign and verify individual
- * data objects.
- */
- #include "Demo.h"
- #include "DemoSignedObject.h"
- #include "DemoSignedObjectDialog.h"
- #include <CCheckBox.h>
- #include <CDataFile.h>
- #include <CDialog.h>
- #include <CDialogText.h>
- #include <CError.h>
- #include <CIconPane.h>
- #include <CRadioGroupPane.h>
- #include <Commands.h>
- #include <Exceptions.h>
- #include <Global.h>
- #include <TBUtilities.h>
-
- #include <OCEErrors.h>
- extern CError *gError;
- extern CursHandle gWatchCursor;
- extern Boolean gHasDigitalSignatureManager;
- void Message(
- const StringPtr textString
- );
-
- /*
- * Imagine that you have written a payroll application and you
- * want to allow your employees to change their authorizations
- * or other information by electronic (AOCE) mail. You would
- * distribute an application that presents a dialog with various
- * checkboxes, edit text fields, and whatnot. The employee would
- * fill in the requisite information and mail the data to the
- * personnel database maintainers.
- *
- * Using the Digital Signature Manager, you can now sign
- * individual fields of the document (in addition to the
- * document itself). This lets you prevent unauthorized
- * modifications to the information, as well as letting you
- * have different fields signed by different individuals.
- * For example, you could allow the employee to select
- * pension distribution or emergency contacts, but require
- * the employee's supervisor's "signature" to change the
- * actual pay rate.
- *
- * This sample shows how the contents of a modal dialog can
- * be signed and verified. It has several checkboxes, radio
- * buttons, and an edit text field. After adjusting the fields
- * "to suit the fashion and the time," the user can then
- * command the following operations:
- *
- * Sign Compute a Digital Signature for
- * the dialog's current contents.
- * Verify Check the current contents against
- * the current signature.
- * Show Signer Show the signer information for the
- * current signature.
- * Save Signature Write the current signature to a
- * designated file.
- * Read Signature Read the designated file to obtain
- * a new, current signature.
- *
- * This demo has been implemented as a Think Class Library
- * Modal Dialog. Internally, it is a normal window and uses
- * the normal window operations. In particular, it does
- * use the standard event loop to handle events. This is
- * essential as the Signer Manager status function calls
- * the Think Class Library event loop, which crashes if
- * there is a modal dialog in the window list.
- */
-
- /*
- * The dialog content. Dialog items have commands
- * that are (item number + kSignedDialogCmdBase).
- * I.e., the OK button has (kSignedDialogCmdBase + dOKButton)
- * Dialog item numbering must, of course, match the
- * numbering in the dialog itself. Also, the way that
- * TCL finds dialog items requires that the radio
- * buttons be last in the list.
- *
- * Note that the dialog Icon item references the "valid
- * signature" icon defined by the Digital Signature Mangager.
- */
- enum {
- dOKButton = 1,
- dFirstButton = dOKButton,
- dSignButton,
- dVerifyButton,
- dSaveSignatureButton,
- dReadSignatureButton,
- dShowSignerButton,
- dLastButton = dShowSignerButton,
- /* Check boxes. Note: the order must agree with gCheckBoxMask[] */
- dAwesomeCheckBox,
- dFirstCheckBox = dAwesomeCheckBox,
- dEtherealCheckBox,
- dExquisiteCheckBox,
- dFabulousCheckBox,
- dFantasticCheckBox,
- dStupendousCheckBox,
- dLastCheckBox = dStupendousCheckBox,
- dCommentaryText,
- dStaticText14,
- dStaticText15,
- dStaticText16,
- dStaticText17,
- dStaticText18,
- dRadioPaneDefinition,
- /* Radio buttons */
- dFlauntRadioButton,
- dFirstRadioButton = dFlauntRadioButton,
- dFloutRadioButton,
- dLastRadioButton = dFloutRadioButton,
- dSignedIcon
- };
- #define CMD(what) (kSignedDialogCmdBase + (what))
-
- /*
- * These are organized in the same order as the
- * checkbox dialog items.
- */
- static unsigned short gCheckBoxMask[] = {
- kAwesomeMask,
- kExquisiteMask,
- kEtherealMask,
- kFabulousMask,
- kFantasticMask,
- kStupendousMask
- };
- #define nCheckBoxes (dLastCheckBox - dFirstCheckBox + 1)
-
- #define itsDialog ((CDialog *) itsWindow)
-
- void
- DemoSignedObjectDialog::IDemoSignedObjectDialog(
- CDirectorOwner *aSupervisor,
- DemoSignedObject *aSignedObject
- )
- {
- inherited::IDLOGDirector(DLOG_DemoSignedObject, aSupervisor);
- itsSignatureValidFlag = FALSE;
- itsSignedObject = aSignedObject;
- AttachCommands();
- SetDialogValues();
- UpdateButtons();
- }
-
- void
- DemoSignedObjectDialog::DoCommand(
- long aCommand
- )
- {
- Boolean wasModal;
-
- /*
- * Make the dialog temporarily non-modal
- * so that the status window appears
- * in front.
- */
- wasModal = itsDialog->IsModal();
- itsDialog->SetModal(FALSE);
- switch (aCommand) {
- case CMD(dSignButton):
- GetDialogValues();
- itsSignatureValidFlag =
- itsSignedObject->SignThisObject();
- UpdateButtons();
- break;
- case CMD(dVerifyButton):
- GetDialogValues();
- itsSignatureValidFlag =
- itsSignedObject->VerifyThisObject();
- UpdateButtons();
- break;
- case CMD(dSaveSignatureButton):
- itsSignedObject->SaveObjectSignature();
- break;
- case CMD(dReadSignatureButton):
- itsSignedObject->ReadObjectSignature();
- itsSignatureValidFlag = FALSE;
- UpdateButtons();
- break;
- case CMD(dShowSignerButton):
- /*
- * The user chose "Show Signer." We'll get an error
- * if we haven't used the signature to verify the
- * dialog. In particular, if you sign the dialog
- * and try to show signer, you'll get this error
- * message. Note that UpdateButtons disables this
- * button except after successful verification.
- */
- TRY {
- itsSignedObject->ShowSigner("\p");
- }
- CATCH {
- if (gLastError == kSIGOperationIncompatibleErr) {
- Message("\pShow signer is valid only after verification");
- NO_PROPAGATE;
- }
- }
- ENDTRY;
- break;
- case CMD(dOKButton):
- aCommand = cmdOK;
- goto doModalAction;
- default:
- itsSignatureValidFlag = FALSE;
- UpdateButtons();
- doModalAction:
- itsDialog->SetModal(wasModal);
- inherited::DoCommand(aCommand);
- break;
- }
- itsDialog->SetModal(wasModal);
- }
-
- Boolean
- DemoSignedObjectDialog::EndDialog(
- long withCommand,
- Boolean fValidate
- )
- {
- Boolean result;
-
- result = inherited::EndDialog(withCommand, fValidate);
- if (withCommand == cmdOK && result == TRUE)
- GetDialogValues();
- }
-
- void
- DemoSignedObjectDialog::GetDialogValues(void)
- {
- register short i;
- unsigned short checkBoxValue;
- CCheckBox *aButton;
- CRadioGroupPane *aRadioGroup;
- CDialogText *someEditText;
- Str255 someText;
-
- checkBoxValue = 0;
- for (i = 0; i < nCheckBoxes; i++) {
- aButton = (CCheckBox *) GetDialogItem(i + dFirstCheckBox);
- if (aButton->IsChecked())
- checkBoxValue |= gCheckBoxMask[i];
- }
- itsSignedObject->SetCheckBoxValues(checkBoxValue);
- aRadioGroup = (CRadioGroupPane *) GetDialogItem(dRadioPaneDefinition);
- itsSignedObject->SetRadioButtonValue(
- aRadioGroup->GetStationID() - dFirstRadioButton + 1
- );
- someEditText = (CDialogText *) GetDialogItem(dCommentaryText);
- someEditText->GetTextString(someText);
- itsSignedObject->SetCommentaryText(someText);
- }
-
- void
- DemoSignedObjectDialog::SetDialogValues(void)
- {
- register short i;
- unsigned short checkBoxValue;
- CCheckBox *aButton;
- CRadioGroupPane *aRadioGroup;
- CEditText *someEditText;
- Boolean isChecked;
- Str255 someText;
-
- checkBoxValue = itsSignedObject->GetCheckBoxValues();
- for (i = 0; i < nCheckBoxes; i++) {
- aButton = (CCheckBox *) GetDialogItem(i + dFirstCheckBox);
- isChecked = ((checkBoxValue & gCheckBoxMask[i]) != 0);
- aButton->SetValue(isChecked);
- }
- aRadioGroup = (CRadioGroupPane *) GetDialogItem(dRadioPaneDefinition);
- aRadioGroup->SetStationID(
- itsSignedObject->GetRadioButtonValue() + dFirstRadioButton - 1
- );
- someEditText = (CEditText *) GetDialogItem(dCommentaryText);
- itsSignedObject->GetCommentaryText(someText);
- someEditText->SetTextPtr((Ptr) &someText[1], someText[0]);
- }
-
- void
- DemoSignedObjectDialog::AttachCommands(void)
- {
- register short i;
-
- for (i = dFirstButton; i <= dLastButton; i++)
- GetDialogItem(i)->SetClickCmd(CMD(i));
- }
-
- void
- DemoSignedObjectDialog::UpdateButtons(void)
- {
- CIconPane *anIconPane;
-
-
- anIconPane = (CIconPane *) GetDialogItem(dSignedIcon);
- /*
- * It might be better to use different icons for
- * "has signature" and "doesn't have signature",
- * but CIconPane doesn't easily lend itself to
- * having multiple icons and I don't really
- * want to have two dialog items on top of
- * each other.
- */
- if (itsSignatureValidFlag)
- anIconPane->Show();
- else {
- anIconPane->Hide();
- }
- if (gHasDigitalSignatureManager == FALSE) {
- GetDialogItem(dSignButton)->Deactivate();
- GetDialogItem(dVerifyButton)->Deactivate();
- GetDialogItem(dShowSignerButton)->Deactivate();
- }
- else {
- if (itsSignedObject->HasSignature()) {
- GetDialogItem(dVerifyButton)->Activate();
- GetDialogItem(dSaveSignatureButton)->Activate();
- }
- else {
- GetDialogItem(dVerifyButton)->Deactivate();
- GetDialogItem(dSaveSignatureButton)->Deactivate();
- }
- if (itsSignedObject->GetContextType() == kSIGVerify
- && itsSignedObject->HasSignature())
- GetDialogItem(dShowSignerButton)->Activate();
- else {
- GetDialogItem(dShowSignerButton)->Deactivate();
- }
- }
- }
-
- CButton *
- DemoSignedObjectDialog::GetDialogItem(
- short dialogID
- )
- {
- return ((CButton *) itsWindow->FindViewByID(dialogID));
- }
-
-
-